package steven.sdc.appPoker.Fragment;

import java.util.ArrayList;

import steven.sdc.appPoker.Adapter.AdapterLichSu;
import steven.sdc.appPoker.Items.ItemLichSu;


import com.amurani.slidingmenu.R;
import android.view.View.OnClickListener;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class FragmentLichSu extends Fragment{
	Button btn_xoalichsu;
	ArrayList<ItemLichSu> items;
	AdapterLichSu adapter;
	ListView listview;
	final CharSequence[] diaglog = {"Xem chi tiết", "Xóa lịch sử trận đấu", "Hủy"};
	final CharSequence co = "Có";
	final CharSequence khong = "Không";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.lich_su, null);
		Init(view);
		return view;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		List();
		ListViewEvent();
		ButtonEvent();

	}
	public void ButtonEvent()
	{
		btn_xoalichsu.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				items.clear();
				adapter.notifyDataSetChanged();
			}
		});
	}
	public void ListViewEvent()
	{
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1,  final int arg2,
					long arg3) {
				
				AlertDialog.Builder alert= new AlertDialog.Builder(getActivity());
				alert.setTitle("Chọn thao tác");
				alert.setItems(diaglog, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if(which == 0){
							
									Fragment frag_chitiet = new FragmentChiTiet();
									getFragmentManager().beginTransaction().add(R.id.fragment_content, frag_chitiet,null).addToBackStack(null).commitAllowingStateLoss();
								
						}
						if(which == 1 ){
							AlertDialog.Builder ok = new AlertDialog.Builder(getActivity());
							ok.setTitle("Bạn có muốn xóa?");
							ok.setPositiveButton(co, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
										items.remove(arg2);
										adapter.notifyDataSetChanged();
										Toast.makeText(getActivity(), "Xóa thành công!!!", Toast.LENGTH_SHORT).show();
									
								}
							});
							ok.setNegativeButton(khong, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
								}
							});
							ok.show();
						}
						if(which == 2){
							
						}
					}
				});
				alert.show();	 
			}
		}) ;
		
	}
	public void List()
	{

		items.add(new ItemLichSu(5,"20-12-2013","Nam"));
		adapter.notifyDataSetChanged();
		
	}
	public void Init(View view)
	{	items = new ArrayList<ItemLichSu>();	
	btn_xoalichsu=(Button)view.findViewById(R.id.btn_xoalichsu);
		listview = (ListView) view.findViewById(R.id.list_lichsu);
		items.add(new ItemLichSu(1,"Aug 4,2013 4:40:31 PM","Bình"));
		items.add(new ItemLichSu(2,"Aug 4,2013 4:40:31 PM","Tiến"));
		items.add(new ItemLichSu(3,"Aug 4,2013 4:40:31 PM","Nhân"));
		items.add(new ItemLichSu(4,"Aug 4,2013 4:40:31 PM","Hoàng"));
		items.add(new ItemLichSu(5,"Aug 4,2013 4:40:31 PM","Nam"));
		items.add(new ItemLichSu(6,"Aug 4,2013 4:40:31 PM","Hiếu"));
		items.add(new ItemLichSu(7,"Aug 4,2013 4:40:31 PM","Trung"));
		items.add(new ItemLichSu(8,"Aug 4,2013 4:40:31 PM","Khang"));
		items.add(new ItemLichSu(9,"Aug 4,2013 4:40:31 PM","Bình"));
		items.add(new ItemLichSu(10,"Aug 4,2013 4:40:31 PM","Sơn"));
		items.add(new ItemLichSu(11,"Aug 4,2013 4:40:31 PM","Luyến"));
		items.add(new ItemLichSu(12,"Aug 4,2013 4:40:31 PM","Khang"));
		adapter = new AdapterLichSu(getActivity(),R.layout.list_item_lichsu1, items);
		listview.setAdapter(adapter);
		
	}
}
